package com.phpxiu.app.utils;

/**
 * @file
 * @brief 星座工具类
 * @author 吴金洲
 * @note 星座计算
 *
 */
public class ConstellationManager {
    /**
     * 星座定义
     */
    public  enum CONSTELLATIONS {
        Capricorn(1, "摩羯座"), Aquarius(2, "水瓶座"), Pisces(3, "双鱼座"), Aries(4,
                "白羊座"), Taurus(5, "金牛座"), Gemini(6, "双子座"), Cancer(7, "巨蟹座"), Leo(
                8, "狮子座"), Virgo(9, "处女座"), Libra(10, "天秤座"), Scorpio(11, "天蝎座"), Sagittarius(
                12, "射手座");

        private CONSTELLATIONS(int code, String chineseName) {
            this.code = code;
            this.chineseName = chineseName;
        }
        private int code;
        private  String chineseName;

        public int getCode() {
            return this.code;
        }

        public String getChineseName() {
            return this.chineseName;
        }
    }

    /**
     * 星座月份划分日期
     */
    public static final int[] CONSTELLATION_DAYS= { 21, 20, 21, 21, 22, 22,23, 24, 24, 24, 23, 22};

    /**
     * 星座集合
     */
    public static final CONSTELLATIONS[] CONSTELLATIONS_LIST= {
            CONSTELLATIONS.Aquarius, CONSTELLATIONS.Pisces, CONSTELLATIONS.Aries,
            CONSTELLATIONS.Taurus, CONSTELLATIONS.Gemini, CONSTELLATIONS.Cancer,
            CONSTELLATIONS.Leo, CONSTELLATIONS.Virgo, CONSTELLATIONS.Libra,
            CONSTELLATIONS.Scorpio, CONSTELLATIONS.Sagittarius,
            CONSTELLATIONS.Capricorn
    };

    /**
     * 根据生日计算星座
     * @param birthday
     * @return
     */
    public static String calculateConstellation(String birthday) {
        if (birthday == null || birthday.trim().length() == 0)
            throw new IllegalArgumentException("the birthday can not be null");
        String[] birthdayElements = birthday.split("-");
        if (birthdayElements.length != 3)
            throw new IllegalArgumentException(
                    "the birthday form is not invalid");
        int month = Integer.parseInt(birthdayElements[1]);
        int day = Integer.parseInt(birthdayElements[2]);
        if (month == 0 || day == 0 || month > 12)
            return "";
        month = day < CONSTELLATION_DAYS[month - 1]?month - 1:month;
        return month > 0?CONSTELLATIONS_LIST[month - 1].getChineseName(): CONSTELLATIONS_LIST[11].getChineseName();
    }
}
